package com.tle202.components;


import android.content.Context;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

import com.tle202.components.CONSTANTS.FONT_PATHS;
import com.tle202.personalinformation.R;

public class TEditText extends EditText {

//	protected final Drawable m_imgCloseButton = getResources().getDrawable(R.drawable.x_button_red_50x50);
	protected final Drawable m_imgCloseButton = new XImage();
	
	protected final String TAG = "TEditText";
	
	
	
	public TEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.initDefaultProperties();
		this.registerListener();
	}
	
	public TEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.initDefaultProperties();
		this.registerListener();
	}
	
	public TEditText(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.initDefaultProperties();
		this.registerListener();
	}
	
	
	
	protected void registerListener () {		
		
		this.addTextChangedListener(new TextListener());
		this.setOnTouchListener(new TouchListener(this));
	}
	
	protected void showHideClearButton (){
				
		Drawable left = this.getCompoundDrawables()[0];
		Drawable top = this.getCompoundDrawables()[1];
		Drawable right = null;
		Drawable bottom = this.getCompoundDrawables()[3];
		
		// Remove clear button
		if (this.getText().toString().isEmpty() == true) {
			
			this.setCompoundDrawables(left, top, right, bottom);
		}
		// Add clear button
		else {
			
			this.setCompoundDrawables(left, top, m_imgCloseButton, bottom);
		}
				
	}// end showHideClearButton
				
	protected void initDefaultProperties () {
		
		Typeface typeface = Typeface.createFromAsset(this.getContext().getAssets(), FONT_PATHS.PLAINT.getValue());
		this.setTypeface(typeface);
		
		float ftFontSize = getResources().getDimension(R.dimen.font_size_normal);
		this.setTextSize(TypedValue.DENSITY_DEFAULT, ftFontSize);
		
		
		
		/* if the Close image is displayed and the user remove his finger from the button, clear it.
		 * Otherwise do nothing
		 */		
		// Set bounds of the Clear button so it will look ok
//		m_imgCloseButton.setBounds(0, 0, m_imgCloseButton.getIntrinsicWidth(), m_imgCloseButton.getIntrinsicHeight());		
		m_imgCloseButton.setBounds(0, 0, Math.round(this.getTextSize()), Math.round(this.getTextSize()));
		
        // There may be initial text in the field, so we may need to display the  button
		showHideClearButton();
	}
	
	
	
	protected class TouchListener implements OnTouchListener {
		
		protected EditText m_parent = null;
		
		public TouchListener(EditText parent) {
			// TODO Auto-generated constructor stub
			m_parent = parent;
		}
		
		@Override
		public boolean onTouch(View view, MotionEvent me) {
			// TODO Auto-generated method stub
			
			if (m_parent.getCompoundDrawables()[2] == null)
                return false;
             
            if (me.getAction() != MotionEvent.ACTION_UP) {
//            	if (me.getX() > m_parent.getWidth() - m_parent.getPaddingRight() - m_imgCloseButton.getIntrinsicWidth()) {
            	if (me.getX() > m_parent.getWidth() - m_parent.getPaddingRight() - m_imgCloseButton.getBounds().width()) {
            		m_parent.setText("");
                	showHideClearButton();
                	return true;
                }
            }
            
            return false;
		}
		
	}
	
	protected class TextListener implements TextWatcher {
		
		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			// TODO Auto-generated method stub
			showHideClearButton();
		}
		
	}
		
}
